//
//  DatedetailsViewController.m
//  geiTA
//
//  Created by 杨元龙 on 14-11-20.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "DatedetailsViewController.h"
#import "LableLableCell.h"
#import "AddViewController.h"
#import "RecordModel.h"
#import "RecordDataBase.h"
#import "TheCountdown.h"
#import "mySinglietongClass.h"
@interface DatedetailsViewController ()<UITableViewDataSource, UITableViewDelegate, AddViewControllerDelegate>

@end

@implementation DatedetailsViewController

-(void)viewWillAppear:(BOOL)animated
{
    self.model1 = [RecordDataBase selectWith:_model1.recordid];
    [self viewDidLoad];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
  
    NSArray *arr2 = [NSArray arrayWithObjects:@"性别", _model1.sex, nil];
    NSMutableArray *arr3 = [NSMutableArray arrayWithObjects:@"手机",_model1.phone, nil];
    NSArray *arr4 = [NSArray arrayWithObjects:@"关系",_model1.relationship, nil];
    NSArray *arr6 = [NSArray arrayWithObjects:@"备注", _model1.note, nil];
    
    
    self.array2 = [NSMutableArray arrayWithObjects:arr2,arr3,arr4,arr6, nil];
    
    
    [self createScroll];
    [self createView];
    [self createTableView];
    [self createBarButton];
}

#pragma mark -
#pragma mark 修改按钮
- (void)createBarButton
{
    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    [button setTitle:@"修改" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithCustomView:button];
    [self.navigationItem setRightBarButtonItem:bar];
}
- (void)buttonAction:(id)sender
{
    mySinglietongClass *my = [mySinglietongClass shareManager];
    my.selectPage = 2;
    AddViewController *add = [[AddViewController alloc]init];
    [self.navigationController pushViewController:add animated:YES];
    [add chuanzhi:_model1];
    add.delegate =self;
}
//协议方法
-(void)huidiao:(RecordModel *)model
{
    
    self.model1 = model;
    
}




#pragma mark -
#pragma mark ScrollView

- (void)createScroll
{
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49)];
     [_scroll setContentSize:CGSizeMake(0, self.view.frame.size.height)];
    [_scroll setBackgroundColor:[UIColor yellowColor]];
    [self.view addSubview:_scroll];
    
   
}

#pragma  mark -
#pragma mark UIView
- (void)createView
{
//    第一个view
    UIView *nameView = [[UIView alloc]initWithFrame:CGRectMake(0, 3, self.view.frame.size.width, self.view.frame.size.height/6)];
    [nameView setBackgroundColor:[UIColor whiteColor]];
    [_scroll addSubview:nameView];
    
    self.photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(20,10,nameView.frame.size.height - 20, nameView.frame.size.height - 20)];
    [_photoImage setBackgroundColor:[UIColor redColor]];
    //照片路径
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[NSString stringWithFormat:@"%ld.png",(long)_model1.recordid]];
    UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
    [_photoImage setImage:savedImage];
    [nameView addSubview:_photoImage];
    
    
    self.nameLable = [[UILabel alloc]initWithFrame:CGRectMake(_photoImage.frame.origin.x + _photoImage.frame.size.width + 20,  _photoImage.frame.origin.y,_photoImage.frame.size.width*2, _photoImage.frame.size.height / 2 - 5)];
    [_nameLable setBackgroundColor:[UIColor blueColor]];
     _nameLable.text = _model1.name;
    [nameView addSubview:_nameLable ];
    
    self.remindLable = [[UILabel alloc]initWithFrame:CGRectMake(_nameLable.frame.origin.x, _nameLable.frame.origin.y + _nameLable.frame.size.height +10, _nameLable.frame.size.width, _nameLable.frame.size.height)];
    _remindLable.text = _model1.remindDate;
    [_remindLable setBackgroundColor:[UIColor yellowColor]];
    [nameView addSubview:_remindLable];
    
    
    //第二个view
    UIView *countdownView =[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/ 6 +6, self.view.frame.size.width, self.view.frame.size.height/ 7)];
    [countdownView setBackgroundColor:[UIColor whiteColor]];
    [_scroll addSubview:countdownView];
    
    UILabel *lable1 = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, countdownView.frame.size.width/2, countdownView.frame.size.height/2- 10)];
    [lable1 setBackgroundColor:[UIColor redColor]];
    [lable1 setText:@"距离今年纪念日还有"];
    [countdownView addSubview:lable1];
    
    self.remindLable1 = [[UILabel alloc]initWithFrame:CGRectMake(lable1.frame.origin.x, countdownView.frame.size.height/2, lable1.frame.size.width, lable1.frame.size.height)];
    [_remindLable1 setBackgroundColor:[UIColor blueColor]];
    
    // 当前年的纪念日
    NSDate *now = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSUInteger unitFlags =NSCalendarUnitYear;
    NSDateComponents *dateComponent = [calendar components:unitFlags fromDate:now];
    NSInteger year = [dateComponent year];
    //字符串拼接
    NSString *monthDay = [_model1.remindDate substringWithRange:NSMakeRange(5, 6)];
    NSString * currYear = [NSString stringWithFormat:@"%ld年%@",(long)year,monthDay ];
    _remindLable1.text = currYear;
    [countdownView addSubview:_remindLable1];
    
    
    
    self.countdownLabel = [[UILabel alloc]initWithFrame:CGRectMake(countdownView.frame.size.width/7 *5,10 , countdownView.frame.size.height/ 4 *3, countdownView.frame.size.height/ 4 *3)];
    [_countdownLabel setBackgroundColor:[UIColor yellowColor]];
       NSString *str = [TheCountdown theCountWith:_model1.remindDate];
    _countdownLabel.text = str;
    
    [countdownView addSubview:_countdownLabel];
    
    
    
    
}


#pragma mark -
#pragma mark 　TableView
- (void)createTableView
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0,self.view.frame.size.height/ 3 + 50, self.view.frame.size.width,self.view.frame.size.height/ 3) style:UITableViewStylePlain];
    [_table setBackgroundColor:[UIColor yellowColor]];
    _table.rowHeight = 70;
    _table.dataSource =self;
    _table.delegate =self;
    _table.scrollEnabled = NO;
    [_scroll addSubview:_table];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_array2 count];
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *pool4 = @"ok4";
    LableLableCell *cell =[tableView dequeueReusableCellWithIdentifier:pool4];
    if (!cell) {
        cell =[[LableLableCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:pool4];
    }
    NSArray *arr = [_array2 objectAtIndex:indexPath.row];
    
    cell.nameLablel.text = [arr objectAtIndex:0];
    

    cell.contentLabel.text = [arr objectAtIndex:1];
    
    NSLog(@"cell:%ld", (long)indexPath.row);
    [_table setFrame:CGRectMake(0,self.view.frame.size.height/ 3 + 50, self.view.frame.size.width, 180 +(_rect1.size.height  + 20) )];
         [_scroll setContentSize:CGSizeMake(0,180 +(_rect1.size.height  + 20+self.view.frame.size.height/ 3 + 50))];
   
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (3 == indexPath.row) {
        
        self.rect1 = [[self.array2[indexPath.row] objectAtIndex:1] boundingRectWithSize:CGSizeMake(self.view.frame.size.width/9 *4, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica-Bold" size:18]} context:nil];
        NSLog(@"%@", [self.array2[indexPath.row] objectAtIndex:1]);
        return _rect1.size.height + 20;
    }
    return 50;
}




- (void)chuangzhi:(RecordModel *)model
{
    self.model1 = model;
}



@end
